package pe.gob.mininter.dirandro.vaadin.panel.bandeja;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import pe.gob.mininter.dirandro.model.Caso;
import pe.gob.mininter.dirandro.model.Opcion;
import pe.gob.mininter.dirandro.service.CasoService;
import pe.gob.mininter.dirandro.util.HarecUtil;
import pe.gob.mininter.dirandro.vaadin.panel.caso.PanelRegistroCaso;
import pe.gob.mininter.dirandro.vaadin.util.DirandroComponent;
import pe.gob.mininter.dirandro.vaadin.util.Injector;
import pe.gob.mininter.dirandro.vaadin.util.TablaFiltro;
import pe.gob.mininter.dirandro.vaadin.util.container.TablaFiltroIndexedContainer;
import pe.gob.mininter.dirandro.vaadin.util.container.TablaFiltroIndexedContainer.TipoComponente;
import pe.gob.mininter.dirandro.vaadin.util.event.TablaFiltroEnterEvent;
import pe.gob.mininter.dirandro.vaadin.util.listener.TablaFiltroEnterListener;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Item;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.event.Action;
import com.vaadin.event.Action.Handler;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class PanelBandejaCasos extends DirandroComponent implements TablaFiltroEnterListener, ClickListener {

	@AutoGenerated
	private VerticalLayout mainLayout;

	@AutoGenerated
	private TablaFiltro tblBandeja;

	@AutoGenerated
	private HorizontalLayout lytBotonera;

	@AutoGenerated
	private Button btnBuscar;

	//CASO
	public static final String COLUMNA_ID = "id";
	public static final String COLUMNA_CASO = "caso";
	public static final String COLUMNA_NUM_CASO = "numeroCaso";
	public static final String COLUMNA_NOMBRE_CASO= "nombreCaso";
	public static final String COLUMNA_SITUACION = "estado";
	public static final String COLUMNA_FECHA_CASO = "fechaCaso";
	
	private static final Action EDITAR_CASO= new Action("Modificar Caso");
	private static final Action[] ITEM_ACTIONS = new Action[] { EDITAR_CASO };
	
	private Action[] action = new Action[] {};

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	/**
	 * 
	 */
	private static final long serialVersionUID = -3860689794611254325L;
	
	private TablaFiltroIndexedContainer container;
	
	private CasoService casoService;
	
	private List<Opcion> acciones;

	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public PanelBandejaCasos(List<Opcion> acciones, String height) {
		super(acciones, height);
		this.acciones=acciones;
		buildMainLayout();
		setCompositionRoot(mainLayout);
		
		casoService  = Injector.obtenerServicio(CasoService.class);
		
		postConstruct();
	}

	@Override
	public void postConstruct() {
		container = new TablaFiltroIndexedContainer();
		
		container.addContainerProperty(COLUMNA_ID, Long.class, StringUtils.EMPTY, "Caso", TipoComponente.TEXT, true, 100);
		container.addContainerProperty(COLUMNA_NUM_CASO, String.class, StringUtils.EMPTY, "Numero de Caso", TipoComponente.TEXT, true, 140);
		container.addContainerProperty(COLUMNA_NOMBRE_CASO, String.class, StringUtils.EMPTY, "Nombre de Caso", TipoComponente.TEXT, true, 130);
		container.addContainerProperty(COLUMNA_SITUACION, String.class, StringUtils.EMPTY, "Situacion", TipoComponente.TEXT, true, 120);
		container.addContainerProperty(COLUMNA_FECHA_CASO, String.class, StringUtils.EMPTY, "Fecha", TipoComponente.DATEPICKER, false, 180);
		container.addContainerProperty(COLUMNA_CASO, Caso.class, null);
		
		tblBandeja.setContainerDataSource(container);
		tblBandeja.setColumnCollapsingAllowed(true);
		tblBandeja.setColumnReorderingAllowed(true);
		tblBandeja.setSelectable(true);
		tblBandeja.addListener((TablaFiltroEnterListener)this);
		
		tblBandeja.setImmediate(true);
		
		buscarHojaRemisiones(new HashMap<String, Object>());
		
		btnBuscar.addListener((ClickListener) this);
		
		tblBandeja.addListener(new ValueChangeListener() {

			/**
			 * 
			 */
			private static final long serialVersionUID = -7070222690010564544L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				
				Integer value = (Integer) event.getProperty().getValue();
				
				tblBandeja.removeAllActionHandlers();
				
				if (value != null) {
					action = ITEM_ACTIONS;
					asignaActionHandler();
				} 

				tblBandeja.requestRepaintTable();
			}
		});		
	}

	private void asignaActionHandler() {
		tblBandeja.addActionHandler(new Handler() {

			/**
			 * 
			 */
			private static final long serialVersionUID = -6373252406881569655L;

			@Override
			public void handleAction(Action action, Object sender, Object target) {

				if (action.equals(EDITAR_CASO)) {
					
					Object objID = tblBandeja.getValue();
					
					Item item = container.getItem(objID);
					
					Caso caso = (Caso)item.getItemProperty(COLUMNA_CASO).getValue();
					
					PanelRegistroCaso pnlRegCaso  = new PanelRegistroCaso(acciones, "-1px");
					pnlRegCaso.setCaso(caso);
					Window wdHojaRemision = new Window();
					
					wdHojaRemision.setModal(false);
					wdHojaRemision.setResizable(false);
					wdHojaRemision.addComponent(pnlRegCaso);
						
					wdHojaRemision.setCaption("Registro de Caso");
					wdHojaRemision.setWidth("900px");
					wdHojaRemision.setHeight("600px");
					getWindow().addWindow(wdHojaRemision);
					
				}
			}

			@Override
			public Action[] getActions(Object target, Object sender) {
				return action;
			}
		});
	}

	private void buscarHojaRemisiones(Map<String, Object> map) {
		
		List<Caso> lCasos = casoService.buscarBandeja(map);
		
		container.removeAllItems();
		int con = 0;
		if(lCasos!=null){
			for (Caso peri : lCasos) {
				Item item = container.addItem(con++);
				
				item.getItemProperty(COLUMNA_ID).setValue(peri.getId());
				item.getItemProperty(COLUMNA_NUM_CASO).setValue(peri.getNroCaso());
				item.getItemProperty(COLUMNA_NOMBRE_CASO).setValue( peri.getNombre());
				item.getItemProperty(COLUMNA_SITUACION).setValue( HarecUtil.valorNombreToEmpty(peri.getSituacion()));
				item.getItemProperty(COLUMNA_FECHA_CASO).setValue(peri.getFecha());
				item.getItemProperty(COLUMNA_CASO).setValue(peri);
				
			}
		
		}
			
	}

	@Override
	public void handleEnterAction(TablaFiltroEnterEvent event) {
		buscarHojaRemisiones(event.getDatos());
	}

	@Override
	public void buttonClick(ClickEvent event) {
		if(event.getButton().equals(btnBuscar)) {
			buscarHojaRemisiones(tblBandeja.getValues());
		}
		
	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		mainLayout.setSpacing(true);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		// lytBotonera
		lytBotonera = buildLytBotonera();
		mainLayout.addComponent(lytBotonera);
		
		// tblBandeja
		tblBandeja = new TablaFiltro();
		tblBandeja.setImmediate(false);
		tblBandeja.setWidth("100.0%");
		tblBandeja.setHeight("100.0%");
		mainLayout.addComponent(tblBandeja);
		mainLayout.setExpandRatio(tblBandeja, 1.0f);
		
		return mainLayout;
	}

	@AutoGenerated
	private HorizontalLayout buildLytBotonera() {
		// common part: create layout
		lytBotonera = new HorizontalLayout();
		lytBotonera.setImmediate(false);
		lytBotonera.setWidth("100.0%");
		lytBotonera.setHeight("-1px");
		lytBotonera.setMargin(false);
		
		// btnBuscar
		btnBuscar = new Button();
		btnBuscar.setCaption("Buscar");
		btnBuscar.setImmediate(true);
		btnBuscar.setWidth("-1px");
		btnBuscar.setHeight("-1px");
		lytBotonera.addComponent(btnBuscar);
		
		return lytBotonera;
	}

}
